package com.schoolhelper.post.service.impl;

import com.schoolhelper.post.domain.Post;
import com.schoolhelper.post.mapper.PostMapper;
import com.schoolhelper.post.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;

@Service
public class PostServiceImpl implements PostService {
    private static final Logger logger = LoggerFactory.getLogger(PostServiceImpl.class);
    @Autowired
    private PostMapper postMapper;

    @PostConstruct
    public void init() {
        logger.info("PostServiceImpl bean initialized");
    }
    @Override
    public ArrayList<Post> getAllPosts() {
        return postMapper.getAllPosts();
    }

    @Override
    public ArrayList<Post> getAllPostsWithPagination(int page, int size) {
        int offset = (page - 1) * size;
        return postMapper.getAllPostsWithPagination(size, offset);
    }

    @Override
    public ArrayList<Post> getPostsByUserId(Long userId) {
        return postMapper.getPostsByUserId(userId);
    }

    @Override
    public ArrayList<Post> getPostsByUserIdWithPagination(Long userId, int page, int size) {
        int offset = (page - 1) * size;
        return postMapper.getPostsByUserIdWithPagination(userId, size, offset);
    }

    @Override
    @Transactional
    public Post createPost(Post post) {
        logger.info("Entering createPost method");
        logger.info("postMapper是否为null: {}", postMapper == null);
        // 设置默认值
        post.setLikeCount(0);
        post.setCommentCount(0);
        post.setIsDeleted(0);
        post.setCreateTime(new Date());
        post.setUpdateTime(new Date());
        logger.info("准备插入帖子数据: {}", post);
        // 插入数据库
        int rowsAffected = postMapper.insertPost(post);
        logger.info("帖子插入操作影响行数: {}", rowsAffected);
        logger.info("插入后post主键id: {}", post.getId());
        if (rowsAffected <= 0) {
            throw new RuntimeException("帖子插入数据库失败");
        }
        // 新增：插入图片信息到 post_image 表
        if (post.getImageUrls() != null && !post.getImageUrls().isEmpty()) {
            for (String url : post.getImageUrls()) {
                postMapper.insertPostImage(post.getId(), url, new Date());
            }
        }
        return post;
    }

    @Override
    @Transactional
    public boolean likePost(Long postId, Long userId) {
        // 检查是否已经点赞
        if (postMapper.isPostLikedByUser(postId, userId) > 0) {
            return false;
        }
        
        // 插入点赞记录
        int result = postMapper.insertPostLike(postId, userId);
        if (result > 0) {
            // 更新帖子点赞数
            postMapper.incrementLikeCount(postId);
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean unlikePost(Long postId, Long userId) {
        // 检查是否已经点赞
        if (postMapper.isPostLikedByUser(postId, userId) == 0) {
            return false;
        }
        
        // 删除点赞记录
        int result = postMapper.deletePostLike(postId, userId);
        if (result > 0) {
            // 更新帖子点赞数
            postMapper.decrementLikeCount(postId);
            return true;
        }
        return false;
    }

    @Override
    public boolean isPostLikedByUser(Long postId, Long userId) {
        return postMapper.isPostLikedByUser(postId, userId) > 0;
    }
}
